package alipay.openplatform.edu.campus.jiuye.service.services.user;

import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.user.UserTopicInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.UserAndTopicPO;
import alipay.openplatform.edu.campus.jiuye.model.po.UserTopicInfoKeyPO;
import alipay.openplatform.edu.campus.jiuye.model.po.UserTopicInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.UserTopicVO;
import alipay.openplatform.edu.campus.jiuye.service.iservice.user.IUserTopicInfoService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserTopicInfoService implements IUserTopicInfoService {
    @Autowired
    private UserTopicInfoMapper userTopicInfoMapper;
    @Autowired
    private TopicInfoMapper topicInfoMapper;

    private static final Logger logger = LoggerFactory.getLogger(UserTopicInfoService.class);

    /**
     * 分页获取用户收藏的主题的列表
     *
     * @return
     */
    public Map getMyTopicByPage(String userId) throws Exception {
        logger.info("分页获取用户收藏的主题的列表|UserTopicInfoService|getMyTopicByPage|params:userId={},pageSize={}", userId);

        //1. 获取总页数
        /*Integer count = userTopicInfoMapper.getMyTopicInfoCount(userId);*/

        //2. 获取当前开始的行的位置
        /*int startPages = (pageNo - 1) * pageSize;
        if (startPages < 0) startPages = 0;*/

        //3. 分页获取数据
        List<UserAndTopicPO> userAndTopicPOList = userTopicInfoMapper.getMyTopicInfoByPage(userId);
        List<UserTopicVO> userTopicInfoVoList = CopyVoPo.copyList(userAndTopicPOList, UserTopicVO.class);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("result", userTopicInfoVoList);

        return result;
    }

    /**
     * 报名或者收藏
     *
     * @param userTopicInfoVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int sendTopic(UserTopicVO userTopicInfoVo) throws InvocationTargetException, IllegalAccessException {
        UserTopicInfoPO userTopicInfoPO = new UserTopicInfoPO();

        CopyVoPo.beanUtilsBean.copyProperties(userTopicInfoPO, userTopicInfoVo);
        logger.info("报名或者收藏|UserTopicInfoService|sendTopic|转vo为po,userTopicInfoPO={},userTopicInfoVo={}", userTopicInfoPO, userTopicInfoVo);

        userTopicInfoMapper.insert(userTopicInfoPO);

        return userTopicInfoPO.getId();
    }

    /**
     * 取消收藏
     *
     * @param userTopicInfoVo
     * @return
     */
    @Transactional
    public int cancelTopic(UserTopicVO userTopicInfoVo) throws InvocationTargetException, IllegalAccessException {
        UserTopicInfoPO userTopicInfoPO = new UserTopicInfoPO();

        CopyVoPo.beanUtilsBean.copyProperties(userTopicInfoPO, userTopicInfoVo);
        logger.info("取消收藏|UserTopicInfoService|cancelTopic|转vo为po,userTopicInfoPO={},userTopicInfoVo={}", userTopicInfoPO, userTopicInfoVo);

        return userTopicInfoMapper.cancelTopic(userTopicInfoPO);
    }

    public UserTopicInfoPO selectIfExist(Map map) {
        return userTopicInfoMapper.selectIfExist(map);
    }

    public UserTopicInfoPO selectByPrimaryKey(UserTopicInfoPO key) {
        return userTopicInfoMapper.selectByPrimaryKey(key);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(UserTopicInfoPO record) {
        return userTopicInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int querySmsIfSend(String userId, Integer topicId) {
        return userTopicInfoMapper.querySmsIfSend(userId, topicId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateIfSMS(String userId, Integer topicId) {
        return userTopicInfoMapper.updateIfSMS(userId, topicId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateTopic(Map map) {
        return userTopicInfoMapper.updateTopic(map);
    }
}